package com.uci.photogam;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class ActivityEditImage extends Activity implements
		ColorPickerDialog.OnColorChangedListener, OnSeekBarChangeListener {

	private static class Size {
		int sample;
		float scale;
	}

	private ViewImageEdit vie;
	private Button edit1;
	private String imgPath;
	public static final String TAG = "EDIT_IMAGE";
	private SharedPreferenceManager spm;
	private RadioGroup rg;

	private Bitmap scaled;
	private int maxWidth;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_edit);
		spm = new SharedPreferenceManager(this.getSharedPreferences(
				V.PREFERENCE, MODE_PRIVATE));
		edit1 = (Button) findViewById(R.id.btnEdit1);
		vie = (ViewImageEdit) findViewById(R.id.editView);
		vie.setBitmap(getIntent().getStringExtra(V.IMAGE_PATH));
		vie.setColor(spm.getInt(V.COLOR, V.COLOR_DEFAULT));
		vie.setStrokeWidth(spm.getInt(V.STROKE, V.STROKE_DEFAULT));
		maxWidth = vie.bitmap.getWidth();
	}

	public void btnActivityEditImageOnClickHandler(View view) {
		switch (view.getId()) {
		case R.id.btnEdit1: // EDIT
			vie.toggleLock();
			edit1.setText((vie.toggleEditImag) ? "Pan Mode" : "Edit Mode");
			break;
		case R.id.btnEdit3: // COLOR
			ColorPickerDialog d = new ColorPickerDialog(this, this, spm.getInt(
					V.COLOR, V.COLOR_DEFAULT));
			d.show();
			break;
		case R.id.btnEdit4: // EDIT Stroke size and pen type
			setupEditDialog();
			break;
		case R.id.btnEdit2: // SHARE
			setupShareDialog();
			break;
		}
	}

	protected void onStop() {
		super.onStop();
		spm.putInt(V.STROKE_WIDTH, V.STROKE_DEFAULT);
		spm.commit();
	}

	protected void onPause() {
		vie.pauseView();
		super.onPause();
	}

	protected void onResume() {
		vie.resumeView();
		super.onResume();
	}

	public void colorChanged(int color) {
		vie.setColor(color);
		spm.putInt(V.COLOR, color);
		spm.commit();
	}

	private void setupEditDialog() {
		final Dialog ed = new Dialog(this);
		ed.setContentView(R.layout.image_edit_tools);
		ed.setTitle("Edit Tools");

		Button save = (Button) ed.findViewById(R.id.btnSaveEditTool);
		final SeekBar strokeBar = (SeekBar) ed
				.findViewById(R.id.seekBarStrokeEditTools);

		strokeBar.setOnSeekBarChangeListener(this);
		strokeBar.setMax(50);
		strokeBar.incrementProgressBy(3);
		strokeBar.setProgress(spm.getInt(V.STROKE, V.STROKE_DEFAULT));
		save.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ed.dismiss();
				spm.putInt(V.STROKE, strokeBar.getProgress());
				spm.putString(V.PEN_TYPE, V.PEN_NORMAL);
				spm.commit();
				vie.setPenType(spm.getString(V.PEN_TYPE, V.PEN_NORMAL));
				vie.setStrokeWidth(strokeBar.getProgress());
			}
		});
		ed.show();
	}

	private void setupShareDialog() {
		final Dialog ed = new Dialog(this);
		ed.setContentView(R.layout.save_img_dialog);
		ed.setTitle("File Name:");

		final EditText fileName = (EditText) ed
				.findViewById(R.id.imgNameEditText);
		
		final Button saveBtn = (Button) ed.findViewById(R.id.btnSave);
		final SeekBar scaleBar = (SeekBar) ed
				.findViewById(R.id.seekBarFileSize);
		
		scaleBar.setOnSeekBarChangeListener(this);
		scaleBar.setMax(maxWidth);
		scaleBar.incrementProgressBy(400);
		scaleBar.setProgress(maxWidth);
//		scaleBar.setProgress(spm.getInt(V.STROKE, V.STROKE_DEFAULT));
		saveBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				saveBtn.setFocusable(true);
				ed.dismiss();
				Log.d("SCALE BY" , String.valueOf(scaleBar.getProgress()));
				String file = fileName.getText().toString()
						.trim();
				
				if (file.length() > 0) {
					String fullPathName = Environment
							.getExternalStorageDirectory()
							.getPath()
							+ "/" + V.APP + "/";
					V.Log("EDIT", fullPathName);
					File dir = new File(fullPathName);
					if (!dir.exists())
						dir.mkdirs();
					File f = new File(fullPathName, file
							+ ".png");
					
					int scaledWidth = scaleBar.getProgress();
					if(scaledWidth < (maxWidth/4))
					{
						scaledWidth = maxWidth/4;
					}
						
					vie.scaleImage(scaledWidth);
					
					try {
						FileOutputStream fos = new FileOutputStream(
								f);
						if (!vie.saveImage(fos))
							V.toastMessage(
									ActivityEditImage.this,
									"image not saved to file");

						// Using the built in send intent
						Intent i = new Intent(
								android.content.Intent.ACTION_SEND);
						i.setType("image/jpeg");
						// Sending this file via the
						// different sending services
						// enabled on phone
						i.putExtra(Intent.EXTRA_STREAM,
								Uri.fromFile(f));
						V.Log("Image location", f.toURI()
								.toString());
						startActivity(i);
					} catch (FileNotFoundException e) {
						V.Log("ViewImageEdit", "File Not Found");
					}
				} else
					V.toastMessage(ActivityEditImage.this,
							"Image Not Save Please Specify File Name");
				finish();
			}
			
		});
		ed.show();
	}

	public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {

	}

	public void onStartTrackingTouch(SeekBar seekBar) {

	}

	public void onStopTrackingTouch(SeekBar seekBar) {

	}

}
